package com.hsav.kcxb.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Space;

import com.hsav.kcxb.R;
import com.hsav.kcxb.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiB on 2017/6/26.
 */

public class ToneAdjuster extends LinearLayout {
    private int toneCount = 7;
    private int lastSelectedTone = -1;
    private List<View> lines = new ArrayList<>(10);

    public ToneAdjuster(Context context) {
        super(context);
        initUI();
    }

    public ToneAdjuster(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initUI();
    }

    public ToneAdjuster(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initUI();
    }

    public void setSelectedTone(int selectedTone)
    {
        if (selectedTone < 0 || selectedTone > toneCount - 1) return;
        if (lastSelectedTone == selectedTone) return;
        if (lastSelectedTone != -1)
            lines.get(lastSelectedTone).setBackgroundResource(R.color.colorPrimaryDark);
        lines.get(selectedTone).setBackgroundResource(R.color.orange);
        lastSelectedTone = selectedTone;
    }

    public void setToneCount(int count)
    {
        removeAllViews();
        lines.clear();
        initUI();
    }

    public int getSelectedTone()
    {
        return lastSelectedTone;
    }

    public void up()
    {
        if (lastSelectedTone == toneCount - 1)
            return;
        setSelectedTone(lastSelectedTone + 1);
    }

    public void down()
    {
        if (lastSelectedTone == 0)
            return;
        setSelectedTone(lastSelectedTone - 1);
    }

    private void initUI() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        int span = DensityUtil.px2dip(getContext(), 4);
        setPadding(span, span, span, span);

        int childCount = toneCount + toneCount + 1;
        for (int i = 0; i < childCount; i++)
        {
            if (i % 2 == 0)
                addView(createSpace());
            else
            {
                View line = createLine(8, 10 + i * 2);
                addView(line);
                lines.add(line);
            }
        }
        setSelectedTone(0);
    }

    private View createSpace() {
        Space space = new Space(getContext());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        space.setLayoutParams(layoutParams);
        return space;
    }

    private View createLine(int width, int height) {
        View view = new View(getContext());
        LayoutParams layoutParams = new LayoutParams(width, height);
        view.setLayoutParams(layoutParams);
        view.setBackgroundResource(R.color.colorPrimaryDark);
        return view;
    }
}
